package com.controller;

import com.entity.Register;
import com.service.impl.RegisterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class userRegisterController {
    @Autowired
//    FUserMapper userMapper;
    RegisterServiceImpl registerService;

    @GetMapping(value = "/zhuce")
    public String register() {
        return "client/zhuce";
    }

    @PostMapping(value = "/toRegister")
    public String toRegister(Register register, Model model) {
        try {
            int row = registerService.getMapper().insert(register, true);
            if (row > 0) {
                model.addAttribute("msg", "注册成功,请登录！");
                return "client/login";
            } else {
                model.addAttribute("error", "注册失败,服务器出错请联系管理员");
            }
        } catch (Exception ex) {
            model.addAttribute("error", "注册失败,已存在" + register.getUsername());
        }
        return "client/register";
    }
}
